package com.yycx.module.bus.provider.service.impl;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.base.service.impl.BaseServiceImpl;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.mybatis.query.CriteriaSave;
import com.yycx.common.mybatis.query.CriteriaUpdate;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.DateUtils;
import com.yycx.common.utils.OrderNoUtil;
import com.yycx.common.utils.SequenceUtils;
import com.yycx.module.admin.client.entity.BaseUser;
import com.yycx.module.bus.client.entity.BusConsumption;
import com.yycx.module.bus.client.entity.BusDept;
import com.yycx.module.bus.client.entity.BusPaymenttype;
import com.yycx.module.bus.provider.mapper.BusConsumptionMapper;
import com.yycx.module.bus.provider.service.BusConsumptionService;
import com.yycx.module.bus.provider.service.BusDeptService;
import com.yycx.module.bus.provider.service.BusPaymentService;
import com.yycx.module.file.provider.service.SysFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 日常开销费用接口实现类
 *
 * @author flyme
 * @date 2020-11-28
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BusConsumptionServiceImpl extends BaseServiceImpl<BusConsumptionMapper, BusConsumption> implements BusConsumptionService {
    @Autowired
    private OrderNoUtil orderNoUtil;

    @Autowired
    private BusPaymentService paymentService;

    @Autowired
    private SysFileService fileService;

    @Resource
    private BusDeptService deptService;

    @Override
    public ResultBody beforeAdd(CriteriaSave cs, BusConsumption consumption, EntityMap extra) {
        Long userId = OpenHelper.getUserId();
        Long companyId = OpenHelper.getCompanyId();
        consumption.setCompanyId(companyId);
        consumption.setConsumptionNo("RC" + getOrderNo(companyId));
        consumption.setApplyUserId(userId);

        return ResultBody.ok();
    }

    private String getOrderNo(Long companyId) {
        CriteriaQuery<BusConsumption> cq = new CriteriaQuery(BusConsumption.class);
        cq.select("consumptionNo");
        cq.eq(true, "companyId", companyId);
        cq.limit(1);
        cq.orderByDesc("createTime");
        String orderNo = getObj(cq, e -> e.toString());
        String date = DateUtils.formatDate(DateUtils.getNowDate(), "MMdd");
        String year = DateUtils.getYear();
        String firstNo = year + date + "001";
        if (FlymeUtils.isNotEmpty(orderNo)) {
            String lastNo = orderNo.substring(10);
            Long no = Long.parseLong(lastNo) + 1;
            return year + date + SequenceUtils.getSequence(no);
        }
        return firstNo;
    }

    @Override
    public ResultBody afterAdd(CriteriaSave cs, BusConsumption busConsumption, EntityMap extra) {
        //添加审批单
        paymentService.addConsumPayment(busConsumption);
        return super.afterAdd(cs, busConsumption, extra);
    }

    @Override
    public ResultBody afterEdit(CriteriaUpdate cu, BusConsumption busConsumption, EntityMap extra) {
        //添加审批单
        paymentService.addConsumPayment(busConsumption);
        return super.afterEdit(cu, busConsumption, extra);
    }


    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<BusConsumption> cq, BusConsumption consumption, EntityMap requestMap) {
        cq.select(BusConsumption.class, "*");
        cq.select(BusDept.class, "deptName");
        cq.addSelect(BaseUser.class, "nickName");
        cq.select(BusPaymenttype.class, "paymentTypeName", "paymentTypeCode");
        cq.createJoin(BusDept.class).setMainField("deptId").setJoinField("deptId");
        cq.eq(BusConsumption.class, "companyId", OpenHelper.getCompanyId());
        cq.createJoin(BusPaymenttype.class).setMainField("paymentTypeId").setJoinField("paymentTypeId");
        cq.orderByDesc("consumption.createTime");
        cq.createJoin(BaseUser.class).setMainField("applyUserId");
        return ResultBody.ok();
    }

    @Override
    public Boolean updateConsumptionCheckState(Long consumptionId, String processInstanceId, Integer checkState) {
        CriteriaUpdate cu = new CriteriaUpdate();
        cu.set(true, "checkState", checkState);
        cu.set(true, "processInstanceId", processInstanceId);
        cu.eq(true, "consumptionId", consumptionId);
        return update(cu);
    }

    @Override
    public EntityMap findByBusConsumptionId(String consumptionId) {
        CriteriaQuery cq = new CriteriaQuery(BusConsumption.class);
        cq.select(BusConsumption.class, "*");
        cq.select(BusDept.class, "deptName");
        cq.eq(BusConsumption.class, "consumptionId", consumptionId);
        cq.createJoin(BusDept.class).setMainField("deptId").setJoinField("deptId");
        return findOne(cq);
    }

    @Override
    public void afterGet(CriteriaQuery cq, EntityMap result) {
        Long consumptionId = result.getLong("consumptionId");
        Long deptId = result.getLong("deptId");
        if (FlymeUtils.isNotEmpty(consumptionId)) {
            BusDept dept = deptService.getById(deptId);
            if (FlymeUtils.isNotEmpty(dept)) {
                result.put("deptName", dept.getDeptName());
            }
            List<EntityMap> fileList = fileService.selectFileListByFileType(consumptionId.toString(), "BusConsumption", "");
            result.put("rcImage", fileList);
        }
        super.afterGet(cq, result);
    }
}
